<?php

/**
 * This is the model class for table "{{kejiban_post}}".
 *
 * The followings are the available columns in table '{{kejiban_post}}':
 * @property integer $id
 * @property integer $user_id
 * @property integer $topic_id
 * @property string $created_time
 */
class Post extends CActiveRecord
{
	public $from, $to, $ngWord;
	
	public $limit = 20, $offset = 0;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Post the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{keijiban_post}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title, content, topic_id, user_id', 'required', 'on'=>'insert'),
			array('user_id, topic_id, visible, created_by_operator', 'numerical', 'integerOnly'=>true),
			array('created_time,image', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, user_id, topic_id, content, visible, created_time, from, to, ngWord, limit, offset', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user'=>array(self::BELONGS_TO, 'User', 'user_id'),
			'topic'=>array(self::BELONGS_TO, 'Topic', 'topic_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'user_id' => Yii::t('app', 'User'),
			'topic_id' => Yii::t('app', 'Topic'),
			'content' => Yii::t('app', 'Content'),
			'created_time' => Yii::t('app', 'Time'),
			'ngWord'=>Yii::t('app', 'Include NG Word'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function mngSearch()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$c=new CDbCriteria;

		if ($this->from) {
			$from = date('Y-m-d',strtotime($this->from));
			$c->addCondition("created_time >= '$from'");
		}

		if ($this->to) {
			$to = date('Y-m-d 23:59:59',strtotime($this->to));
			$c->addCondition("created_time <= '$to'");
		}

		$c->compare('topic_id',$this->topic_id);
		$c->compare('content',$this->ngWord, true);
		$c->order = 'created_time DESC';

		return new CActiveDataProvider($this, array(
			'criteria'=>$c,
		));
	}
	
	public function getSearchSummaryMng(){
		$c=new CDbCriteria;

		if ($this->from) {
			$from = date('Y-m-d',strtotime($this->from));
			$c->addCondition("created_time >= '$from'");
		}

		if ($this->to) {
			$to = date('Y-m-d 23:59:59',strtotime($this->to));
			$c->addCondition("created_time <= '$to'");
		}

		$c->compare('topic_id',$this->topic_id);
		$c->compare('content',$this->ngWord, true);
		$c->with = 'user';
		$c->select = 'DISTINCT t.user_id';

		// user
		$c1 = clone $c;
		$c1->compare('user.role_id', Role::ROLE_USER);
		
		// operator
		$c2 = clone $c;
		$c2->compare('user.role_id', Role::ROLE_OPERATOR);
		
		$res = array(
			'user' => $this->count($c1),
			'opr' => $this->count($c2),
		);
		return $res;
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$c=new CDbCriteria;
		$p = $this->tableName();
		$c->join = "LEFT OUTER JOIN $p p ON t.user_id=p.user_id AND t.created_time < p.created_time";
		$c->addCondition("p.id is null");

		if ($this->from) {
			$from = date('Y-m-d',strtotime($this->from));
			$c->addCondition("t.created_time >= '$from'");
		}

		if ($this->to) {
			$to = date('Y-m-d 23:59:59',strtotime($this->to));
			$c->addCondition("t.created_time <= '$to'");
		}

		$c->compare('t.topic_id',$this->topic_id);
		$c->order = 't.created_time DESC';

		return new CActiveDataProvider($this, array(
			'criteria'=>$c,
		));
	}

	public function getSearchSummary(){
		$c=new CDbCriteria;
		if ($this->from) {
			$from = date('Y-m-d',strtotime($this->from));
			$c->addCondition("t.created_time >= '$from'");
		}

		if ($this->to) {
			$to = date('Y-m-d 23:59:59',strtotime($this->to));
			$c->addCondition("t.created_time <= '$to'");
		}
		
		$c->compare('topic_id',$this->topic_id);
		$c->with = 'user';
		$c->select = 'DISTINCT t.user_id';

		// user
		$c1 = clone $c;
		$c1->compare('user.role_id', Role::ROLE_USER);
		
		// operator
		$c2 = clone $c;
		$c2->compare('user.role_id', Role::ROLE_OPERATOR);
		
		$res = array(
			'user' => $this->count($c1),
			'opr' => $this->count($c2),
		);
		return $res;
	}
	
	public function searchApi()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$c=new CDbCriteria;

		$c->with = 'user';
		$c->compare('topic_id',$this->topic_id);
		$c->compare('user_id',$this->user_id);
		$c->compare('visible',$this->visible);
		$c->order = 't.created_time DESC';
		$c->limit = $this->limit;
		$c->offset = $this->offset;

		return $this->findAll($c);
	}
	
}